package com.myapp.ruyixing.http;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.myapp.ruyixing.model.PushMessage;
import com.myapp.ruyixing.model.PushMessage.Message;

public class PushMessageParser extends RYJSONParser {
	@Override
	public Object parse(JSONObject jsonObject) {
		PushMessage pushMessage = new PushMessage();
		pushMessage.data = new ArrayList<PushMessage.Message>();
		if (!ModelParser.parseModel(pushMessage, jsonObject)) {
			return null;
		}
		JSONArray array = jsonObject.optJSONArray("data");
		for (int i = 0; array != null && i < array.length(); i++) {
			JSONObject object = array.optJSONObject(i);
			if (object != null) {
				Message message = new Message();
				message.title = object.optString("title", "");
				message.url = object.optString("url", "");
				pushMessage.data.add(message);
			}
		}

		return pushMessage;
	}
}
